package com.cskaoyan.controller.mall;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.PageDataVo;
import com.cskaoyan.bean.mall.order.ShipBO;
import com.cskaoyan.bean.mall.order.*;
import com.cskaoyan.service.mall.OrderService;
import com.cskaoyan.service.mall.ReplyComment;
import com.cskaoyan.utils.Constant;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("admin/order")
public class OrderController {
    @Autowired
    OrderService orderService;

    @RequiresPermissions("admin:order:delete")
    @GetMapping("list")
    public BaseRespVo queryOrderList(BaseParam baseParam, OrderBO orderBO) {
        List<Order> orders = orderService.queryOrderList(baseParam, orderBO);
        return BaseRespVo.ok(PageDataVo.ok(orders));
    }

    @GetMapping("channel")
    public BaseRespVo queryChannel() {
        List<Channel> channels = orderService.queryChannel();
        return BaseRespVo.ok(channels);
    }

    @GetMapping("detail")
    public BaseRespVo queryOrderDetail(Integer id) {
        OrderDetail orderDetail = orderService.queryOrderDetail(id);
        if (orderDetail != null) return BaseRespVo.ok(orderDetail);
        return BaseRespVo.error();
    }

    @RequiresPermissions("admin:order:delete")
    @PostMapping("delete")
    public BaseRespVo deleteOrder(@RequestBody Map<String, Integer> map) {
        int code = orderService.deleteOrder(map.get("orderId"));
        if (code == Constant.SUCCESS) return BaseRespVo.ok();
        return BaseRespVo.error();
    }

    @RequiresPermissions("admin:order:refund")
    @PostMapping("refund")
    public BaseRespVo refundMoney(@RequestBody RefundMoney refundMoney) {
        int code = orderService.refundMoney(refundMoney);
        if (code == Constant.SUCCESS) return BaseRespVo.ok();
        return BaseRespVo.error();
    }

    @RequiresPermissions("admin:order:ship")
    @PostMapping("ship")
    public BaseRespVo ship(@RequestBody ShipBO shipBO) {
        int code = orderService.shipUpdateOrder(shipBO);
        if (code == Constant.SUCCESS) return BaseRespVo.ok();
        return BaseRespVo.error();
    }

    @RequiresPermissions("admin:order:reply")
    @PostMapping("reply")
    public BaseRespVo replyComment(@RequestBody ReplyComment replyComment) {
        int code = orderService.replyComment(replyComment);
        if (code == Constant.SUCCESS) return BaseRespVo.ok();
        return BaseRespVo.error();
    }
}
